package io.kiki.sba.registry.common.model;


import io.kiki.sba.registry.common.model.dataserver.DatumSummary;
import io.kiki.sba.registry.common.model.slot.filter.SyncAcceptorRequest;
import io.kiki.sba.registry.common.model.slot.filter.SyncSlotAcceptorManager;
import io.kiki.sba.registry.common.model.store.Publisher;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Setter
@Getter
public final class PublisherUtils {
    private PublisherUtils() {
    }

    /**
     * change publisher word cache
     */
    public static Publisher internPublisher(Publisher publisher) {
        publisher.setRegisterId(publisher.getRegisterId());
        publisher.setDataInfoId(publisher.getDataInfoId());
        publisher.setInstanceId(publisher.getInstanceId());
        publisher.setGroup(publisher.getGroup());
        publisher.setDataId(publisher.getDataId());
        publisher.setClientId(publisher.getClientId());
        publisher.setZone(publisher.getZone());
        publisher.setProcessId(publisher.getProcessId());
        publisher.setAppName(publisher.getAppName());
        return publisher;
    }

    public static Map<String, DatumSummary> getDatumSummary(Map<String, Map<String, Publisher>> publisherMap, SyncSlotAcceptorManager syncSlotAcceptorManager) {
        Map<String, DatumSummary> map = new HashMap<>(publisherMap.size());
        for (Map.Entry<String, Map<String, Publisher>> entry : publisherMap.entrySet()) {

            // filter dataInfoId
            if (!syncSlotAcceptorManager.accept(SyncAcceptorRequest.buildRequest(entry.getKey()))) {
                continue;
            }

            // filter publisher
            map.put(entry.getKey(), DatumSummary.of(entry.getKey(), entry.getValue(), syncSlotAcceptorManager));
        }
        return map;
    }

}
